//
//  UIViewController+Helper.m
//  BaseLib
//
//  Created by CPX on 2018/9/18.
//

#import "UIViewController+Helper.h"

/**
 控制器帮助类
 */
@implementation UIViewController (Helper)

+ (UIViewController *)currentViewController

{
    
    UIViewController *resultVC = [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    while (resultVC.presentedViewController) {
        
        resultVC = [self topViewController:resultVC.presentedViewController];
        
    }
    
//    NSLog(@"当前viewController===========%@",resultVC);
    
    return resultVC;
    
}

+ (UIViewController *)topViewController:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UINavigationController class]]) {
        
        return [self topViewController:[(UINavigationController *)vc topViewController]];
        
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        
        return [self topViewController:[(UITabBarController *)vc selectedViewController]];
        
    } else {
        
        return vc;
        
    }
    
    return nil;
}

@end
